$(function () {
	$.get({
		type:"GET",
		url:baseURL+'sys/user/selectOne',
		success:function (r) {
			var user = r.sysUser;
			var todyTime=new Date();
			var oldTime=new Date(user.createTime);
			var days=(todyTime.getTime()-oldTime.getTime())%(24*60*60*1000)==0?(todyTime.getTime()-oldTime.getTime())/(24*60*60*1000):((todyTime.getTime()-oldTime.getTime())/(24*60*60*1000)+1).toFixed(0)
			$("#name").text(user.username)
			$("#interval").text(todyTime.getHours()>12?'下午好':'早上好')
			$("#welcome").text("今天是你加入黔云医院第"+days+"天")
			$("#img").attr('src',user.src);
			$("#user_id").val(user.userId);
			$("#username").val(user.username);
			$("#deptName").val(user.deptName);
			$("#gender").val(user.gender=0?'女':'男');
			$("#mobile").val(user.mobile);
			$("#synopsis").text(user.synopsis);
			$("#merit").text(user.merit);
		}
	})
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		user: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.user = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.user.id == null ? "doctor/user/save" : "doctor/user/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.user),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "doctor/user/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "doctor/user/info/"+id, function(r){
                vm.user = r.user;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});